package com.threeox.commonlibrary.ui.view.reload;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.threeox.commonlibrary.R;
import com.threeox.commonlibrary.entity.engine.ReloadType;
import com.threeox.commonlibrary.ui.view.inter.reload.IReloadView;
import com.threeox.commonlibrary.ui.view.inter.reload.OnReloadListener;
import com.threeox.utillibrary.util.res.LayoutUtils;

/**
 *
 * @ClassName: CommonReloadView
 *
 * @Description: Todo(类库的各种状态加载的View)
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2017/4/29 下午10:06
 *
 * @version 1.0
 */
@SuppressLint("NewApi")
public class CommonReloadView extends LinearLayout implements IReloadView, View.OnClickListener {

    private OnReloadListener mOnReloadListener = null;

    /*====================加载动画的View======================*/
    private LoadingView mLoadingView;
    private ImageView mOvalShadeView;
    private LinearLayout mLoadingLayout;
    private TextView mLoadingTextView;
    /*====================加载动画的View======================*/
    /*====================加载动画======================*/
    private ObjectAnimator rectAnimator, ovalAnimator;
    private ObjectAnimator rectUpAnimator, rectRotateAnimator;
    /*====================加载动画======================*/

    /*====================没有数据的View======================*/
    private TextView mNotDataTextView;
    private LinearLayout mNotDataLayout;
    private ImageView mNotDataImageView;
    /*====================没有书局的View======================*/

    /*====================没有网络的View======================*/
    private TextView mNotNetWorkTextView;
    private LinearLayout mNotNetWorkLayout;
    private ImageView mNotNetWorkImageView;
    /*====================没有网络的View======================*/

    /*====================请求失败的View======================*/
    private TextView mRequestErrorTextView;
    private LinearLayout mRequestErrorLayout;
    private ImageView mRequestErrorImageView;
    /*====================请求失败的View======================*/

    private int mFallDistance;
    private boolean isShowAnimator = false;// 是否已经开启动画，默认没有

    public CommonReloadView(Context context) {
        this(context, null);
    }

    public CommonReloadView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonReloadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutUtils.inflate(context, R.layout.view_reload, this);
        initLoadView();
        initNotDataView();
        initNotNetWorkView();
        initRequestErrorView();
        initListener();
        mFallDistance = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 70, getContext().getResources().getDisplayMetrics());

    }

    /**
     * 初始化加载动画的View
     */
    private void initLoadView() {
        mLoadingView = (LoadingView) findViewById(R.id.loading_view);
        mOvalShadeView = (ImageView) findViewById(R.id.bottom_shadow);
        mLoadingLayout = (LinearLayout) findViewById(R.id.loading_layout);
        mLoadingTextView = (TextView) findViewById(R.id.loading_textview);
    }

    /**
     * 初始化没有数据的View
     */
    private void initNotDataView() {
        mNotDataLayout = (LinearLayout) findViewById(R.id.not_data_layout);
        mNotDataTextView = (TextView) findViewById(R.id.not_data_textview);
        mNotDataImageView = (ImageView) findViewById(R.id.not_data_imageview);
    }

    /**
     * 初始化没有网络的View
     */
    private void initNotNetWorkView() {
        mNotNetWorkTextView = (TextView) findViewById(R.id.not_network_textview);
        mNotNetWorkLayout = (LinearLayout) findViewById(R.id.not_network_layout);
        mNotNetWorkImageView = (ImageView) findViewById(R.id.not_network_imageview);
    }

    /**
     * 初始化请求失败的View
     */
    private void initRequestErrorView() {
        mRequestErrorTextView = (TextView) findViewById(R.id.request_error_textview);
        mRequestErrorLayout = (LinearLayout) findViewById(R.id.request_error_layout);
        mRequestErrorImageView = (ImageView) findViewById(R.id.request_error_imageview);
    }

    /**
     * 初始化点击事件
     *
     */
    private void initListener() {
        mNotDataLayout.setOnClickListener(this);
        mNotNetWorkLayout.setOnClickListener(this);
        mRequestErrorLayout.setOnClickListener(this);
    }

    /**
     * 下落的动画
     */
    private void startFall() {
        if (null == rectAnimator) {
            rectAnimator = ObjectAnimator.ofFloat(mLoadingView, "translationY", mFallDistance,0);
            rectAnimator.setInterpolator(new DecelerateInterpolator());
        }
        if (null == ovalAnimator) {
            ovalAnimator = ObjectAnimator.ofFloat(mOvalShadeView, "scaleX", 1.0f,0.3f);
         }
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(1000);
        animatorSet.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                startUp();
            }
        });
        animatorSet.play(rectAnimator).with(ovalAnimator);
        animatorSet.start();
    }

    /**
     * 弹跳起来的动画
     */
    private void startUp() {
        if (null == rectUpAnimator) {
            rectUpAnimator = ObjectAnimator.ofFloat(mLoadingView, "translationY", 0,mFallDistance);
            rectUpAnimator.setInterpolator(new AccelerateInterpolator());
        }
        if (null == rectRotateAnimator) {
            rectRotateAnimator = ObjectAnimator.ofFloat(mLoadingView, "rotation", 0,180);
        }
        /*if (mLoadingView.getCurrentShape() == LoadingView.Shape.RACTANGLE) {//如果是三角形，反向旋转
            rectRotateAnimator = ObjectAnimator.ofFloat(mLoadingView, "rotation", 0,-180);
        }*/
        ObjectAnimator ovalAnimator = ObjectAnimator.ofFloat(mOvalShadeView, "scaleX", 0.3f,1.0f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(1000);
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                //在弹跳动画完成之后，调用changeShape，该方法会重新设置当前需要绘制的图形，并重绘该图形
                mLoadingView.changeShape(mLoadingView.getCurrentShape());
                //交错执行上升和下落的动画
                startFall();
            }
        });
        animatorSet.play(rectUpAnimator).with(rectRotateAnimator).with(ovalAnimator);
        animatorSet.start();
    }

    @Override
    public synchronized void requestIn(String message) {
        if (!isShowAnimator){
            this.isShowAnimator = true;
            startFall();
        }
        mNotDataLayout.setVisibility(GONE);
        mNotNetWorkLayout.setVisibility(GONE);
        mRequestErrorLayout.setVisibility(GONE);

        mLoadingLayout.setVisibility(VISIBLE);
        mLoadingTextView.setText(message);
    }

    @Override
    public void requestFault(Object data, String msg) {
        mNotDataLayout.setVisibility(GONE);
        mLoadingLayout.setVisibility(GONE);
        mNotNetWorkLayout.setVisibility(GONE);

        mRequestErrorLayout.setVisibility(VISIBLE);
        mRequestErrorTextView.setText(msg);
    }

    @Override
    public void requestSucceed(Object data) {
        mNotDataLayout.setVisibility(GONE);
        mLoadingLayout.setVisibility(GONE);
        mNotNetWorkLayout.setVisibility(GONE);
        mRequestErrorLayout.setVisibility(GONE);
    }

    @Override
    public void notData(String message) {
        mLoadingLayout.setVisibility(GONE);
        mNotNetWorkLayout.setVisibility(GONE);
        mRequestErrorLayout.setVisibility(GONE);

        mNotDataLayout.setVisibility(VISIBLE);
        mNotDataTextView.setText(message);
    }

    @Override
    public void networkClose(String message) {
        mNotDataLayout.setVisibility(GONE);
        mLoadingLayout.setVisibility(GONE);
        mRequestErrorLayout.setVisibility(GONE);

        mNotNetWorkLayout.setVisibility(VISIBLE);
        mNotNetWorkTextView.setText(message);
    }

    @Override
    public void networkWeak(String message) {
        mNotDataLayout.setVisibility(GONE);
        mLoadingLayout.setVisibility(GONE);
        mRequestErrorLayout.setVisibility(GONE);

        mNotNetWorkLayout.setVisibility(VISIBLE);
        mNotNetWorkTextView.setText(message);

    }

    @Override
    public void setOnReloadListener(OnReloadListener onReloadListener) {
        this.mOnReloadListener = onReloadListener;
    }

    @Override
    public void onClick(View view) {
        if (mOnReloadListener != null) {
            ReloadType reloadType = ReloadType.OTHER;
            int id = view.getId();
            if (R.id.not_data_layout == id) {
                reloadType = ReloadType.NOTDATA;
            } else if (R.id.not_network_layout == id) {
                reloadType = ReloadType.NOTNETWORK;
            } else if (R.id.request_error_layout == id) {
                reloadType = ReloadType.REQUESTERROR;
            }
            mOnReloadListener.onClick(view, reloadType);
        }
    }

    /*======================公开各种View=========================*/

    public TextView getNotDataTextView() {
        return mNotDataTextView;
    }

    public LinearLayout getNotDataLayout() {
        return mNotDataLayout;
    }

    public ImageView getNotDataImageView() {
        return mNotDataImageView;
    }

    public TextView getNotNetWorkTextView() {
        return mNotNetWorkTextView;
    }

    public LinearLayout getNotNetWorkLayout() {
        return mNotNetWorkLayout;
    }

    public ImageView getNotNetWorkImageView() {
        return mNotNetWorkImageView;
    }

    public TextView getRequestErrorTextView() {
        return mRequestErrorTextView;
    }

    public LinearLayout getRequestErrorLayout() {
        return mRequestErrorLayout;
    }

    public ImageView getRequestErrorImageView() {
        return mRequestErrorImageView;
    }

    /*======================公开各种View=========================*/

}
